package com.larno.larnosqliteexample.database.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.larno.larnosqliteexample.database.bean.Todo;
import com.larno.larnosqliteexample.database.provider.TodoContentProvider;
import com.larno.larnosqliteexample.database.tables.TodoTable;

/**
 * Created by sks on 2016/7/4.
 */
public class TodoDao {
    public static Uri insert(Context context, Todo todo) {
        ContentValues values = new ContentValues();
        values.put(TodoTable.COLUMN_CATEGORY, todo.category);
        values.put(TodoTable.COLUMN_SUMMARY, todo.summary);
        values.put(TodoTable.COLUMN_DESCRIPTION, todo.description);
        Uri uri = context.getContentResolver().insert(TodoContentProvider.CONTENT_URI, values);
        return uri;
    }

    public static int update(Context context, Uri uri, Todo todo) {
        ContentValues values = new ContentValues();
        values.put(TodoTable.COLUMN_CATEGORY, todo.category);
        values.put(TodoTable.COLUMN_SUMMARY, todo.summary);
        values.put(TodoTable.COLUMN_DESCRIPTION, todo.description);
        int rowsUpdated = context.getContentResolver().update(uri, values, null, null);
        return rowsUpdated;
    }

    public static int delete(Context context, Uri uri) {
        int rowsDeleted = context.getContentResolver().delete(uri, null, null);
        return rowsDeleted;
    }

    public static Cursor query(Context context,  String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor = context.getContentResolver().query(TodoContentProvider.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
        return cursor;
    }

}
